/**
 * 用户中心--发票--发票信息管理
 */
import {Message} from "element-ui"
import {
    addInvoiceInfo,
    deleteInvoinceInfo,
    getInvoiceInfoList,
    updateInfoStatus,
    updateInvoiceInfo
} from "../../../../api"

export default {
    name: "invoice-info",
    data() {
        let phone = (rule, value, callback) => {
            if (!value) {
                return callback(new Error("请输入手机号"))
            } else {
                const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
                if (reg.test(value)) {
                    callback()
                } else {
                    return callback(new Error("请输入正确的手机号"))
                }
            }
        }
        let bankNum = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('开户行账号不能为空'))
            } else {
                const pattern = /^([1-9]{1})(\d{14}|\d{18})$/
                if (pattern.test(value)) {
                    callback()
                } else {
                    return callback(new Error("请输入正确的银行卡号"))
                }
            }
        }
        let invoiceTitle = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('不能为空'))
            } else {
                callback()
            }
        }
        let taxNum = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('纳税号不能为空'))
            } else {
                let regArr = [/^[\da-z]{10,15}$/i, /^\d{6}[\da-z]{10,12}$/i, /^[a-z]\d{6}[\da-z]{9,11}$/i, /^[a-z]{2}\d{6}[\da-z]{8,10}$/i, /^\d{14}[\dx][\da-z]{4,5}$/i, /^\d{17}[\dx][\da-z]{1,2}$/i, /^[a-z]\d{14}[\dx][\da-z]{3,4}$/i, /^[a-z]\d{17}[\dx][\da-z]{0,1}$/i, /^[\d]{6}[\da-z]{13,14}$/i],
                    j = regArr.length
                for (let i = 0; i < j; i++) {
                    if (regArr[i].test(value)) {
                        callback()
                    } else {
                        return callback(new Error("请输入正确的纳税号"))
                    }
                }
            }
        }
        return {
            radio: 3,
            invoiceInfoData: [],
            title: '新增发票信息',
            deleteDialogVisible: false,//删除弹窗
            waitDeleteInfoId: null,//待删除发票信息id
            unfoldFlag: false,//收起非必填项
            invoiceInfo: {
                invoiceType: '增值税普通发票',
                makeInvoice: 1,
                invoiceTitle: '',
                taxNum: '',
                status: false,
            },
            rules: {
                fixedLineTelephone: [
                    {validator: phone, trigger: 'blur'}
                ],
                bankNum: [
                    {validator: bankNum, trigger: 'blur'}
                ],
                taxNum: [
                    {required: true, validator: taxNum, trigger: 'blur'}
                ],
                invoiceTitle: [
                    {required: true, validator: invoiceTitle, trigger: 'blur'}
                ],
            }
        }
    },
    created() {
        this.searchInvoiceInfoList()
    },
    methods: {
        //返回
        goBack() {
            //返回发票首页
            this.$emit('moduleIndexChange', 'home')
        },
        //新增
        saveInvoiceInfo() {
            this.$refs.invoiceInfo.validate(valid => {
                if (valid) {
                    this.invoiceInfo.status = this.invoiceInfo.status ? 1 : 0
                    if (this.invoiceInfo.id){
                        updateInvoiceInfo(this.invoiceInfo).then(res=>{
                            if (res.data.code === 200) {
                                this.$message({
                                    message: '修改成功',
                                    type: 'success'
                                })
                            } else {
                                this.$message({
                                    message: res.data.msg,
                                    type: 'warning'
                                })
                            }
                            this.searchInvoiceInfoList()
                            this.handleCancel()
                        }).catch(err=>{
                            this.$message.error("修改失败！")
                            this.searchInvoiceInfoList()
                            this.handleCancel()
                        })
                    }else{
                        addInvoiceInfo(this.invoiceInfo).then(res => {
                            if (res.data.code === 200) {
                                this.$message({
                                    message: '新增成功',
                                    type: 'success'
                                })
                            } else {
                                this.$message({
                                    message: res.data.msg,
                                    type: 'warning'
                                })
                            }
                            this.searchInvoiceInfoList()
                            this.handleCancel()
                        }).catch(() => {
                            this.$message.error("新增失败！")
                            this.searchInvoiceInfoList()
                            this.handleCancel()
                        })
                    }

                } else {
                    return false
                }
            })

        },
        //显示删除弹窗
        showDeleteDialog(row) {
            this.deleteDialogVisible = true
            this.waitDeleteInfoId = row.id
        },
        //修改
        modification(row) {
            Message.warning("请修改信息后，点击保存！")
            this.title = '修改发票信息'
            this.invoiceInfo = row
        },

        // 取消
        handleCancel() {
            this.title = '新增发票信息'
            this.invoiceInfo = {
                invoiceType: '增值税普通发票',
                makeInvoice: 1,
                invoiceTitle: '',
                taxNum: '',
                status: false,
            }
        },

        //查询发票信息列表
        searchInvoiceInfoList() {
            let param = {
                status: null
            }
            getInvoiceInfoList(param).then(res => {
                if (res.data.code === 200) {
                    this.invoiceInfoData = res.data.result
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
            }).catch(() => {
                // this.$message.error("获取列表失败！")
            })
        },
        //设为默认抬头
        setDefault(row) {
            let param = {
                status: 1,
                id: row.id
            }
            updateInfoStatus(param).then(res => {
                if (res.data.code === 200) {
                    this.searchInvoiceInfoList()
                    this.$message({
                        message: '修改成功',
                        type: 'success'
                    })
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
            }).catch(() => {
                // this.$message.error("修改失败！")
            })
        },
        //删除发票信息
        deleteInvoinceInfo() {
            let param = {
                id: this.waitDeleteInfoId
            }
            deleteInvoinceInfo(param).then(res => {
                if (res.data.code === 200) {
                    this.searchInvoiceInfoList()
                    this.$message({
                        message: '删除成功',
                        type: 'success'
                    })
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
            }).catch(() => {
                // this.$message.error("修改失败！")
            })
        },

    }
}
